package com.daily.pump.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ProgressBar;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.daily.pump.R;
import com.daily.pump.app.App;
import com.daily.pump.constants.ExtraConstants;
import com.daily.pump.model.Device;
import com.daily.pump.model.SimpleWifiBean;
import com.xunxi.mimi.utils.ToastUtil;


/**
 * Desc:我的-设置视频聊天价格
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class LoginDialog extends DialogFragment {

    private ScanDialogListener mScanDialogListener;

    private String mDeviceName;

    private String mLoginName;

    private String mLoginPwd;

    private EditText mNameEt;

    private EditText mPwdEt;

    private ProgressBar mProgressBar;

    private Handler mHandler = new Handler();

    public void setScanDialogListener(ScanDialogListener listener) {
        this.mScanDialogListener = listener;
    }

    public static LoginDialog newInstance(String scanResult) {
        LoginDialog loginDialog = new LoginDialog();
        Bundle bundle = new Bundle();
        bundle.putString(ExtraConstants.DEVICE_NAME, scanResult);
        loginDialog.setArguments(bundle);
        return loginDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dismissAllowingStateLoss();
                if (mScanDialogListener != null) {
                    mScanDialogListener.onDismiss();
                }
            }
        });
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_login, container, false);
        initData(dialogView);
        return dialogView;
    }

    private void initData(View dialogView) {
        mNameEt = dialogView.findViewById(R.id.name_et);
        String defaultName = "CUSTOM";
        mNameEt.setText(defaultName);
        mNameEt.setSelection(defaultName.length());
        mPwdEt = dialogView.findViewById(R.id.pwd_et);
        mProgressBar = dialogView.findViewById(R.id.progress);
        dialogView.findViewById(R.id.modify_pwd_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
                if (mScanDialogListener != null) {
                    mScanDialogListener.showModifyPwdDialog();
                }
            }
        });
        dialogView.findViewById(R.id.login_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLogin();
            }
        });

        Bundle bundle = getArguments();
        if (bundle != null) {
            mDeviceName = bundle.getString(ExtraConstants.DEVICE_NAME);
        }
    }

    private void onLogin() {
        String name = mNameEt.getText().toString().trim();
        String password = mPwdEt.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.show(getActivity(), "please input login name");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.show(getActivity(), "please input password");
            return;
        }

        if (password.length() < 3) {
            ToastUtil.show(getActivity(), "password at least 3 characters");
            return;
        }

        mProgressBar.setVisibility(View.VISIBLE);

        this.mLoginName = name;
        this.mLoginPwd = password;
        if (mScanDialogListener != null) {
            mScanDialogListener.onLogin(name, password);
        }
        if (mHandler != null) {
            mHandler.removeCallbacks(mLoginRunnable);
            mHandler.postDelayed(mLoginRunnable, 10 * 1000);
        }
    }

    private Runnable mLoginRunnable = new Runnable() {
        @Override
        public void run() {
            hideProgress();
        }
    };

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, LoginDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    public void hideProgress() {
        if (mHandler != null) {
            mHandler.removeCallbacks(mLoginRunnable);
        }
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.GONE);
        }
        ToastUtil.show(getActivity(), "login fail");
    }

    public Device saveLoginDevice(SimpleWifiBean loginWifiBean, String deviceName) {
        Device device = new Device();
        device.deviceName = deviceName;
        device.loginName = mLoginName;
        device.password = mLoginPwd;
        device.wifiBean = loginWifiBean;
        App.getInstance().setLoginDevice(device);
        return device;
    }

    public Device modifyLoginDevice(SimpleWifiBean loginWifiBean, String deviceName, Device modifyDevice) {
        Device device = new Device();
        device.deviceName = deviceName;
        device.loginName = mLoginName;
        device.password = mLoginPwd;
        device.wifiBean = loginWifiBean;
        App.getInstance().modifyLoginDevice(modifyDevice, device);
        return device;
    }

    @Override
    public void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacks(mLoginRunnable);
        }
        super.onDestroy();
    }

    public interface ScanDialogListener {
        void onLogin(String name, String password);

        void onDismiss();

        void showModifyPwdDialog();
    }
}

